package com.xinfu.newproject.util;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatUtils {
    private static String str;
    private static String str2;
    private static long millionSeconds;
    private static long millionSeconds2;
    private static String str3;
    private static String normalStr;
    private static Date date;
    private static Date targetDate;
    private static String strdate;

    //传格式
    public static String getNormalTimeString(String time, String inFormat, String outFormat) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat(inFormat);
            //毫秒
            long millions = sdf1.parse(time).getTime();
            SimpleDateFormat sdf = new SimpleDateFormat(outFormat);
            //格式化字符串，输出字符串
            normalStr = sdf.format(millions);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return normalStr;
    }

    public static String getTime(Long time) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            // 直接把long格式化,如果是字符串还要进行new date();

            str = sdf.format(time);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return str;
    }

    public static String getTimeSS(Long time) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 直接把long格式化,如果是字符串还要进行new date();

            str = sdf.format(time);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return str;
    }

    public static String getDate(Long time) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // 直接把long格式化,如果是字符串还要进行new date();

            str = sdf.format(time);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return str;
    }

    public static String getMonth(Long time) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            // 直接把long格式化,如果是字符串还要进行new date();

            str = sdf.format(time);
        } catch (Exception e) {

            e.printStackTrace();
        }

        return str;
    }

    public static long getLongMills(String time) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //毫秒
            millionSeconds = sdf.parse(time).getTime();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return millionSeconds;
    }

    public static long getLongMillsForyyyyMMddHHmm(String time) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
            //毫秒
            millionSeconds2 = sdf.parse(time).getTime();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return millionSeconds2;
    }

    public static long getLongMillsForymhmHanzi(String time) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
            //毫秒
            millionSeconds = sdf.parse(time).getTime();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return millionSeconds;
    }

    public static String getTimeString(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            // 直接把long格式化,如果是字符串还要进行new date();

            str2 = sdf.format(date);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return str2;
    }

    public static String getYearMonthDayString(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // 直接把long格式化,如果是字符串还要进行new date();

            str2 = sdf.format(date);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return str2;
    }

    public static String getYearMonthDayCNString(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            // 直接把long格式化,如果是字符串还要进行new date();

            str2 = sdf.format(date);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return str2;
    }

    //传入日期date，得到指定输出格式
    public static String getTargetStrForDate(Date date, String outFormat) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(outFormat);

            strdate = sdf.format(date);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return strdate;
    }

    //年月日
    public static String getYearMonthDayHourMinute(Date date) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 直接把long格式化,如果是字符串还要进行new date();

            str3 = sdf.format(date);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return str3;
    }

    public static Date getDate(String YearMonthDay) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            date = sdf.parse(YearMonthDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date getTargetDate(String input, String inFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(inFormat);
        try {
            targetDate = sdf.parse(input);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return targetDate;
    }

    /**
     * date2比date1多的天数(不满24小时的也算一天)
     *
     * @param date_before
     * @param date_after
     * @return
     */
    public static int differentDays(Date date_before, Date date_after) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date_before);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date_after);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    public static int differentDays(Date date_before, long mills_after) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = getDate(mills_after);
        Date date_after = new Date();
        try {
            date_after = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date_before);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date_after);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2 - day1));
            return day2 - day1;
        }
    }

    /**
     * 判断给定时间与当前时间相差多少天
     * 不满24小时的  不 算一天
     *
     * @param date 日志
     * @return //正数表示在当前时间之后，负数表示在当前时间之前
     */
    public static long getDistanceDays(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        long days = 0;
        try {
            Date time = df.parse(date);//String转Date
            Date now = new Date();//获取当前时间
            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            Log.e("tag_time", ": " + diff);

            days = diff / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;//正数表示在当前时间之后，负数表示在当前时间之前
    }

    public static long getDistanceDaysWithMills(long mills) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(mills);
        long days = 0;
        try {
            Date time = df.parse(date);//String转Date
            Date now = new Date();//获取当前时间
            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            days = diff / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;//正数表示在当前时间之后，负数表示在当前时间之前
    }

    /**
     * 判断time是否在now的n天之内(判断某个时间是否是在条件时间的n天之内
     * 正数表示在条件时间n天之后，负数表示在条件时间n天之前)
     *
     * @param time
     * @param now
     * @param n    正数表示在条件时间n天之后，负数表示在条件时间n天之前
     * @return
     */
    public static boolean belongDate(Date time, Date now, int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();  //得到日历
        calendar.setTime(now);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, n);
        Date before7days = calendar.getTime();   //得到n前的时间
        if (before7days.getTime() < time.getTime()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断time是否在from，to之内
     *
     * @param time 指定日期
     * @param from 开始日期
     * @param to   结束日期
     * @return
     */
    public static boolean belongCalendar(Date time, Date from, Date to) {
        Calendar date = Calendar.getInstance();
        date.setTime(time);

        Calendar after = Calendar.getInstance();
        after.setTime(from);

        Calendar before = Calendar.getInstance();
        before.setTime(to);

        if (date.after(after) && date.before(before)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 时间相减
     *
     * @param date
     * @return
     */
    /**
     * 时间相减
     *
     * @param date
     * @return
     */
    public static String getDistanceDays2(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date days = null;
        long dayss = 0;
        long hours = 0;
        long minutes = 0;
        long diff = 0;
        try {
            Date time = df.parse(date);//String转Date
            Date now = new Date();//获取当前时间
            long time1 = time.getTime();
            long time2 = now.getTime();
//            long ssss = sdf1.parse("1970").getTime();
            diff = time2 - time1;
//            Log.e("tag_time", ": " + diff);
            if (diff < 0){
                return "0";
            }else {
                dayss = diff / (1000 * 60 * 60 * 24);
                hours = (diff - dayss * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
                minutes = (diff - dayss * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000* 60);
            }
//            days = diff / (60 * 24);
            days = new Date(diff);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (dayss == 0) {
            if (hours > 0) {
                return hours + "小时" + minutes + "分钟";
            } else {
                return minutes+"分钟";
            }
        }
        return "" + dayss + "天" + hours + "小时" + minutes + "分钟";//正数表示在当前时间之后，负数表示在当前时间之前
    }

}
